import procontroll.*; // XBOX CONTROLLER Library
import net.java.games.input.*; // No clue what this library is for??
import processing.net.*; // Processing network library
import processing.serial.*; // Serial for communication with Arduino

ControllIO ControllIO; 
ControllDevice Controller;

Server s;
Server s2;
Client c;
Serial myPort;

ControllSlider L3Y;
ControllSlider R3Y;

float totalLeftY;
float totalRightY;

void setup(){
  size(450, 255);
  background(204);
  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++frameRate(20); // Slow it down a little
  
  myPort = new Serial(this, "COM4", 9600); // Create instance for serial port
    
  ControllIO = ControllIO.getInstance(this);
  Controller = ControllIO.getDevice("Controller (XBOX 360 For Windows)");
  Controller.setTolerance(0.3f);
  
  L3Y = Controller.getSlider(0);
  R3Y = Controller.getSlider(2);
  
  L3Y.setMultiplier(100);
  R3Y.setMultiplier(100);
  
  s = new Server(this, 12345); // Start a simple server on a port
  s2 = new Server(this, 54321); // Start a simple server on a port
  
  // Small Actuator
  Controller.plug(this, "PressA", ControllIO.ON_PRESS, 0);
  Controller.plug(this, "ReleaseA", ControllIO.ON_RELEASE, 0);
  Controller.plug(this, "PressB", ControllIO.ON_PRESS, 1);
  Controller.plug(this, "ReleaseB", ControllIO.ON_RELEASE, 1);
  
  // Big Actuator
  Controller.plug(this, "PressX", ControllIO.ON_PRESS, 2);
  Controller.plug(this, "ReleaseX", ControllIO.ON_RELEASE, 2);
  Controller.plug(this, "PressY", ControllIO.ON_PRESS, 3);
  Controller.plug(this, "ReleaseY", ControllIO.ON_RELEASE, 3);
  
  // Brush
  Controller.plug(this, "PressLB", ControllIO.ON_PRESS, 4);
  //Controller.plug(this, "ReleaseLB", ControllIO.ON_RELEASE, 4);
  Controller.plug(this, "PressRB", ControllIO.ON_PRESS, 5);
  //Controller.plug(this, "ReleaseRB", ControllIO.ON_RELEASE, 5);
}

void draw(){
  totalRightY = (map(R3Y.getValue(), -100, 100, 3, 125));
  totalLeftY = (map(L3Y.getValue(), -100, 100, 130, 253));

  myPort.write(int(totalRightY)); // Writes data to pin 13 (tx)
  myPort.write(int(totalLeftY));
  
  println("Left Stick Value: " + int(totalLeftY) + " Right Stick Value: " + int(totalRightY));
}

// Small Actuator
void PressA(){
  myPort.write(128);
}
void ReleaseA(){
  myPort.write(127);
}
void PressB(){
  myPort.write(126);
}
void ReleaseB(){
  myPort.write(127);
}

// Big Actuator
void PressX(){
  myPort.write(255);
}
void ReleaseX(){
  myPort.write(254);
}
void PressY(){
  myPort.write(129);
}
void ReleaseY(){
  myPort.write(254);
}

// Brush
void PressLB(){
  myPort.write(1);
}
void PressRB(){
  myPort.write(2);
}
